﻿@*
//------------------------------------------------------------------------------
//  此代码版权声明为全文件覆盖，如有原作者特别声明，会在下方手动补充
//  此代码版权（除特别声明外的代码）归作者本人Diego所有
//  源代码使用协议遵循本仓库的开源协议及附加协议
//  Gitee源代码仓库：https://gitee.com/diego2098/ThingsGateway
//  Github源代码仓库：https://github.com/kimdiego2098/ThingsGateway
//  使用文档：https://diego2098.gitee.io/thingsgateway-docs/
//  QQ群：605534569
//------------------------------------------------------------------------------
*@

@page "/admin/role"
@using System.Linq.Expressions;
@using BlazorComponent;
@using Masa.Blazor.Presets;
@using Microsoft.AspNetCore.Authorization;
@using ThingsGateway.Admin.Application;

@namespace ThingsGateway.Admin.Blazor
@attribute [Authorize]
@inject UserResoures UserResoures
@inherits BaseComponentBase
@layout MainLayout
<AppDataTable @ref="_datatable" TItem="SysRole" SearchItem="RolePageInput" AddItem="RoleAddInput" EditItem="RoleEditInput" 
              SearchModel="@_search" IsShowSearchKey
               QueryCallAsync="QueryCallAsync" AddCallAsync="AddCallAsync"
               EditCallAsync="EditCallAsync" DeleteCallAsync="DeleteCallAsync"
                IsShowQueryButton
               IsShowAddButton=@UserResoures.IsHasButtonWithRole("sysroleadd")
               IsShowDeleteButton=@UserResoures.IsHasButtonWithRole("sysroledelete")
               IsShowEditButton=@UserResoures.IsHasButtonWithRole("sysroleedit")>

     <AddTemplate>
         @GetRenderFragment(context)

     </AddTemplate>

     <EditTemplate>
         @GetRenderFragment(context)
     </EditTemplate>

     <ItemColOperTemplate>
         <MList Dense>
            @if (@UserResoures.IsHasButtonWithRole("sysroleperresuorce"))
            {
                <MListItem OnClick="async()=>
                    {
                   _choiceRoleId=context.Item.Id;
                    await ResuorceInitAsync();
                    _isShowResuorces=true;
                    }">
                    <MListItemTitle Class="ml-2">资源权限</MListItemTitle>
                </MListItem>

            }
            @if (@UserResoures.IsHasButtonWithRole("sysroleperuser"))
            {
                <MListItem OnClick="async()=>
                    {
                   _choiceRoleId=context.Item.Id;
                    await UserInitAsync();
                   _isShowUsers=true;
                    }">
                    <MListItemTitle Class="ml-2">授权用户</MListItemTitle>
                </MListItem>

            }
        </MList>
    </ItemColOperTemplate>

</AppDataTable>

<PDrawer @bind-Value="_isShowResuorces" OnCancel="() => _isShowResuorces = false"
         Title=资源授权
         Width=@(IsMobile?"100%":"600")
         MaxWidth="600" OnSave="OnRoleHasResuorcesSaveAsync">
    @if (_isShowResuorces)
    {
        <MSheet Outlined Class="ma-0 pa-2">
             <MRow Align="AlignTypes.Center">
                 <MCol>  <MLabel Class="ml-4 font-weight-black">菜单</MLabel>  </MCol>
                 <MDivider Vertical />
                 <MCol>  <MLabel Class="ml-4 font-weight-black">按钮</MLabel>    </MCol>
             </MRow>
         </MSheet>
        @foreach (var menu in _resTreeSelectors)
        {
            <MSheet Outlined Class="ma-0 pa-4">
                 <MRow Align="AlignTypes.Center">
                     <MCol>
                         <MListItem IsActive=@(_roleHasResuorces.Any(it=>it.MenuId==menu.Id))>
                             <ItemContent>
                                 <MListItemContent>
                                     <MListItemTitle>@menu.Title</MListItemTitle>
                                 </MListItemContent>
                                 <MListItemAction>
                                     <MCheckbox TValue=bool Value="@context.Active" ValueChanged=@(enable=>
                                       {
                                       if(!enable)
                                       _roleHasResuorces.RemoveWhere(it=>it.MenuId==menu.Id);
                                       else if(!_roleHasResuorces.Any(it=>it.MenuId==menu.Id))
                                       _roleHasResuorces.Add(new() {MenuId=menu.Id});
                                       }
                                       )></MCheckbox>
                                 </MListItemAction>
                             </ItemContent>
                         </MListItem>
                     </MCol>
                     <MDivider Vertical />
                     <MCol>
                         @GetButtonCore(menu)
                     </MCol>

                 </MRow>

             </MSheet>
        }

    }

</PDrawer>

<PDrawer @bind-Value="_isShowUsers" OnCancel="() => _isShowUsers = false"
         Title=授权用户
         Width=@(IsMobile?"100%":"500")
         MaxWidth="500" OnSave="OnUsersSaveAsync">

    <MCard Flat Class="ma-0 pa-4">
         <MCardTitle Class="py-2">
             <MTextField Dense Style="max-width:200px;" HideDetails=@("auto") Class="my-1 mx-2 ml-6" @bind-Value="_searchKey"
                        Outlined Label=@typeof(SysUser).GetDescription(nameof(SysUser.Account)) />
         </MCardTitle>
         <MTreeview Class="my-1" Dense OpenAll TItem="UserSelectorOutput" TKey="UserSelectorOutput" Selectable @bind-Value="_usersChoice"
                    Items="_allUsers" ItemText="r=>r.Account" ItemChildren="r=>null"
                    ItemKey=@(r=>r)>
             <LabelContent>
                 <span title=@context.Item.Account>
                     @context.Item.Account
                 </span>
             </LabelContent>
         </MTreeview>
     </MCard>

 </PDrawer>


 @code {
    RenderFragment GetButtonCore(RoleGrantResourceMenu menu)
    {
        RenderFragment ViewSubMenu = null;

        foreach (var button in menu.Button ?? new())
        {
            ViewSubMenu +=
    @<MListItem Class="ml-6" IsActive=@(_roleHasResuorces.FirstOrDefault(it=>it.MenuId==menu.Id)?.ButtonInfo?.Contains(button.Id)==true)>
        <ItemContent>
            <MListItemContent>
                <MListItemTitle>@button.Title</MListItemTitle>
            </MListItemContent>
            <MListItemAction>
                <MCheckbox TValue=bool Value="@context.Active" ValueChanged=@(a=>
                       {
                       if(!a)
                       {
                       _roleHasResuorces.FirstOrDefault(it=>it.MenuId==menu.Id)?.ButtonInfo?.RemoveWhere(it=>it==button.Id);
                       }
                       else
                       {
                       if( !(_roleHasResuorces.FirstOrDefault(it=>it.MenuId==menu.Id)?.ButtonInfo?.Any(it=>it==button.Id)==true))
                       {
                       if(!_roleHasResuorces.Any(it=>it.MenuId==menu.Id))
                       {
                       _roleHasResuorces.Add(new() {MenuId=menu.Id});
                       }
                       _roleHasResuorces.FirstOrDefault(it=>it.MenuId==menu.Id).ButtonInfo.Add(button.Id);
                       }
                       }
                       })></MCheckbox>
            </MListItemAction>
        </ItemContent>
    </MListItem>
        ;
        }

        return ViewSubMenu;
    }

    RenderFragment GetRenderFragment(RoleAddInput context)
    {
        RenderFragment renderFragment =
    @<div>
        <MSubheader Class="mt-4 font-weight-black">    @(context.Description(x => x.Name))  </MSubheader>
        <MTextField Dense Outlined HideDetails="@("auto")" @bind-Value=@context.Name />

         <MSubheader Class="mt-4 mb-5 font-weight-black">@(context.Description(x => x.SortCode)) </MSubheader>
        <MSlider @bind-Value=@context.SortCode Class="mb-5" TValue=int ThumbLabel="@("always")" Dense />
     </div>
    ;
        return renderFragment;
    }

}

